const express = require('express');
const router = express.Router();
const Refrigerator = require('../models/Refrigerator');

// 获取所有冰箱
router.get('/', async (req, res) => {
  try {
    const refrigerators = await Refrigerator.getAllRefrigerators();
    res.json({
      success: true,
      data: refrigerators,
      total: refrigerators.length
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '获取冰箱列表失败',
      error: error.message
    });
  }
});

// 获取活跃状态的冰箱
router.get('/active', async (req, res) => {
  try {
    const refrigerators = await Refrigerator.getActiveRefrigerators();
    res.json({
      success: true,
      data: refrigerators,
      total: refrigerators.length
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '获取活跃冰箱失败',
      error: error.message
    });
  }
});

// 根据位置获取冰箱
router.get('/location/:location', async (req, res) => {
  try {
    const { location } = req.params;
    const refrigerators = await Refrigerator.getRefrigeratorsByLocation(location);
    res.json({
      success: true,
      data: refrigerators,
      total: refrigerators.length
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '根据位置获取冰箱失败',
      error: error.message
    });
  }
});

// 根据ID获取冰箱详情（包含食物统计）
router.get('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const refrigerator = await Refrigerator.getRefrigeratorWithStats(id);
    
    if (!refrigerator) {
      return res.status(404).json({
        success: false,
        message: '冰箱不存在'
      });
    }

    res.json({
      success: true,
      data: refrigerator
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '获取冰箱详情失败',
      error: error.message
    });
  }
});

// 获取冰箱中的食物数量
router.get('/:id/food-count', async (req, res) => {
  try {
    const { id } = req.params;
    const foodCount = await Refrigerator.getRefrigeratorFoodCount(id);
    res.json({
      success: true,
      data: foodCount
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '获取冰箱食物数量失败',
      error: error.message
    });
  }
});

// 搜索冰箱
router.get('/search/:keyword', async (req, res) => {
  try {
    const { keyword } = req.params;
    const refrigerators = await Refrigerator.searchRefrigerators(keyword);
    res.json({
      success: true,
      data: refrigerators,
      total: refrigerators.length
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '搜索冰箱失败',
      error: error.message
    });
  }
});

// 创建冰箱
router.post('/', async (req, res) => {
  try {
    const refrigeratorData = req.body;
    
    // 验证必填字段
    if (!refrigeratorData.name || !refrigeratorData.location) {
      return res.status(400).json({
        success: false,
        message: '冰箱名称和位置为必填字段'
      });
    }

    const refrigerator = await Refrigerator.createRefrigerator(refrigeratorData);
    res.status(201).json({
      success: true,
      message: '冰箱创建成功',
      data: refrigerator
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '创建冰箱失败',
      error: error.message
    });
  }
});

// 更新冰箱
router.put('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const refrigeratorData = req.body;
    
    // 验证必填字段
    if (!refrigeratorData.name || !refrigeratorData.location) {
      return res.status(400).json({
        success: false,
        message: '冰箱名称和位置为必填字段'
      });
    }

    const refrigerator = await Refrigerator.updateRefrigerator(id, refrigeratorData);
    if (!refrigerator) {
      return res.status(404).json({
        success: false,
        message: '冰箱不存在'
      });
    }

    res.json({
      success: true,
      message: '冰箱更新成功',
      data: refrigerator
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '更新冰箱失败',
      error: error.message
    });
  }
});

// 删除冰箱
router.delete('/:id', async (req, res) => {
  try {
    const { id } = req.params;
    const success = await Refrigerator.deleteRefrigerator(id);
    
    if (!success) {
      return res.status(404).json({
        success: false,
        message: '冰箱不存在或删除失败'
      });
    }

    res.json({
      success: true,
      message: '冰箱删除成功'
    });
  } catch (error) {
    if (error.message.includes('仍有食物存储')) {
      return res.status(400).json({
        success: false,
        message: error.message
      });
    }
    
    res.status(500).json({
      success: false,
      message: '删除冰箱失败',
      error: error.message
    });
  }
});

// 获取冰箱统计信息
router.get('/stats/overview', async (req, res) => {
  try {
    const stats = await Refrigerator.getRefrigeratorStats();
    res.json({
      success: true,
      data: stats
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '获取冰箱统计失败',
      error: error.message
    });
  }
});

module.exports = router;
